package com.sunlong.jiangwei.web;

import com.sunlong.jiangwei.entity.JwRole;
import com.sunlong.jiangwei.entity.result.Result;
import com.sunlong.jiangwei.service.IRoleService;
import com.sunlong.jiangwei.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jiangwei/role")
@Slf4j
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    public Result<List<JwRole>> listResult(){
        List<JwRole> powers = roleService.findAllPowers();
        return ResultUtil.data(powers);
    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = roleService.add(map);
        if (null != errorMap && errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    @PostMapping("/delete")
    public Result<Void> delete(@RequestBody Map<String,Object> map){
        if (!map.containsKey("id")){
            return ResultUtil.error("参数缺失：id");
        }
        Map<String,String> errorMap = roleService.delete(map);
        if (null != errorMap && errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    @PostMapping("/modify")
    public Result<Void> modify(@RequestBody JwRole role){
        Map<String,String> errorMap = roleService.modify(role);
        if (null != errorMap && errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }
}
